//
//  AlarmConditionView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/15.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "AlarmConditionView.h"
#import "CusTableViewCell.h"
#import "HistogramViewCell.h"
#import "ChartTableModel.h"


@interface AlarmConditionView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *alarmTableView;
@property (nonatomic,strong)NSArray *alertCountList;
@property (nonatomic,strong)NSArray *alertSummation;
@property (nonatomic,assign)BOOL allowLoad;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end
static NSString *HistogramViewCellID = @"HistogramViewCellID";
static NSString *CusTableViewCellID = @"CusTableViewCellID";
@implementation AlarmConditionView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _allowLoad = NO;
        [self setUpUI];
        _needRefresh = YES;
    }
    return self;
}
- (void)setUpUI
{
    _alarmTableView = [UITableView new];
    _alarmTableView.dataSource = self;
    _alarmTableView.delegate = self;
    _alarmTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _alarmTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _alarmTableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [_alarmTableView registerClass:[HistogramViewCell class] forCellReuseIdentifier:HistogramViewCellID];
    [_alarmTableView registerClass:[CusTableViewCell class] forCellReuseIdentifier:CusTableViewCellID];
    [self addSubview:_alarmTableView];
    [_alarmTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
//    [self loadDataWithParam:nil];
    
}
- (void)loadNewData
{
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AlertStatisByDay method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        NSDictionary *dict = responseObject[@"obj"];
        if ([responseObject[@"state"] integerValue] == 0) {
            self.alertSummation = dict[@"alertSummation"];
            self.alertCountList = dict[@"alertCountList"];
            _allowLoad = YES;
            [_alarmTableView reloadData];
        }
         [_alarmTableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
         [_alarmTableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
    
}
- (void)loadDataBackGroundWithParam:(NSDictionary *)param
{
    if (_needRefresh) {
       [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AlertStatisByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            NSDictionary *dict = responseObject[@"obj"];
            if ([responseObject[@"state"] integerValue] == 0) {
                self.alertSummation = dict[@"alertSummation"];
                self.alertCountList = dict[@"alertCountList"];
                _allowLoad = YES;
                
                _needRefresh = NO;
            }
            [_alarmTableView reloadData];
            [_alarmTableView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_alarmTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}

- (void)loadDataWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
       [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AlertStatisByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSDictionary *dict = responseObject[@"obj"];
            if ([responseObject[@"state"] integerValue] == 0) {
                self.alertSummation = dict[@"alertSummation"];
                self.alertCountList = dict[@"alertCountList"];
                _allowLoad = YES;
                
                _needRefresh = NO;
                
            }
            [_alarmTableView reloadData];
            [_alarmTableView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_alarmTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
       return 2;
    }else{
        return 0;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 261;
    }else{
        return 355;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        HistogramViewCell *histogramCell = [HistogramViewCell new];
        histogramCell.title = @"报警统计";
        if (self.alertCountList) {
            [histogramCell laodUIWithDataArr:self.alertCountList];
        }
        return histogramCell;
    }else{
        CusTableViewCell *cusCell = [CusTableViewCell new];
        cusCell.title = @"报警提示";
        cusCell.leftString = @"报警车辆";
        cusCell.centerString = @"报警类型";
        cusCell.rightString = @"报警时间";
        if(self.alertSummation){
            NSMutableArray *tempArr = [NSMutableArray array];
            for (int i=0; i<self.alertSummation.count; i++) {
                NSDictionary *dic = self.alertSummation[i];
                ChartTableModel *model = [ChartTableModel new];
                model.leftString = [NSString stringWithFormat:@"%@",dic[@"plateNum"]];
                model.centerString = [NSString stringWithFormat:@"%@",dic[@"alertEvent"]];
                model.rightString = [NSString stringWithFormat:@"%@",dic[@"time"]];
                [tempArr addObject:model];
            }
            [cusCell laodTableWithDataArr:tempArr.copy];
        }
        return cusCell;
    }
}

@end
